package com.zsj;


import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

import java.io.*;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangsj
 * @date 2020/5/26 19:17
 */
public class JsCssCompress {
    public static void main(String[] args) {
//        String dirPath = "D:\\workFile\\WuJing\\code\\ops\\Jxop\\npage";
//        String dirPath = "C:\\Users\\zhang\\Desktop\\src";
//        String dirPath = "D:\\workFile\\WuJing\\code\\ops\\JxopCC\\npage";
        String dirPath = "D:\\workFile\\LeJiaXueChe\\Code\\ops\\Jxop\\npage";
        File file = new File(dirPath);
        traverseDirFile(file);
    }

    /**
     * 遍历文件
     *
     * @param file {@link File}
     */
    public static void traverseDirFile(File file) {
        File[] files = file.listFiles();
        if (files == null) {
            System.out.println("没有文件");
            System.exit(1);
        }
        for (File f : files) {
            //若是目录，则递归打印该目录下的文件
            if (f.isDirectory()) {
                traverseDirFile(f);
            }
            //若是文件，直接打印
            if (f.isFile()) {
                System.out.println("【检测文件】 【" + f.getName() + "】" + "  【" + f + "】");
                compressCssJsFile(f);
            }
        }
    }

    public static void compressCssJsFile(final File file) {
        final String fileName = file.getName();
        if (fileName.contains("min") || fileName.contains("index2") || fileName.contains("index3")
                || fileName.contains("echarts")) {
            System.out.println("【【文件已压缩】】");
            System.out.println("");
            System.out.println("");
            System.out.println("");
        } else if (fileName.contains("js")) {
            System.out.println("》》》》》》》》》》》》【开始压缩 js 文件】 》》》》》》》》》》》");

            try {
                InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
                JavaScriptCompressor javaScriptCompressor = new JavaScriptCompressor(in, new ErrorReporter() {
                    public void warning(String message, String sourceName,
                                        int line, String lineSource, int lineOffset) {
                        System.err.println("\n[WARNING] in " + file);
                        if (line < 0) {
                            System.err.println("  " + message);
                        } else {
                            System.err.println("  " + line + ':' + lineOffset + ':' + message);
                        }
                    }

                    public void error(String message, String sourceName,
                                      int line, String lineSource, int lineOffset) {
                        System.err.println("[ERROR] in " + file);
                        if (line < 0) {
                            System.err.println("  " + message);
                        } else {
                            System.err.println("  " + line + ':' + lineOffset + ':' + message);
                        }
                    }

                    public EvaluatorException runtimeError(String message, String sourceName,
                                                           int line, String lineSource, int lineOffset) {
                        error(message, sourceName, line, lineSource, lineOffset);
                        return new EvaluatorException(message);
                    }
                });

                String path = file.getPath().substring(0, file.getPath().lastIndexOf("\\"));
                String outName = fileName.substring(0, fileName.lastIndexOf(".")) + ".min.js";
                String outPath = path + "\\" + outName;
                System.out.println("《《《《《《《《《《《《 写入文件【" + file.getPath() + "】");

                FileOutputStream fileOutputStream = new FileOutputStream(file.getPath());
                OutputStreamWriter out = new OutputStreamWriter(fileOutputStream, "UTF-8");

                javaScriptCompressor.compress(out, -1, true, false, false, false);

                // 关闭
                in.close();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }

            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("");
        } else if (fileName.contains("css")) {
            System.out.println("》》》》》》》》》》》》【开始压缩 css 文件】 》》》》》》》》》》》");
            System.out.println("");
            System.out.println("");
        } else {
            System.out.println("");
            System.out.println("");
            System.out.println("");
        }
    }
}
